package stud.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import stud.aistar.entity.Image;
import stud.aistar.entity.User;
import stud.aistar.service.IImagesService;
import stud.aistar.service.IMedicalVoService;
import stud.aistar.util.DateUtil;
import stud.aistar.util.QiniuCloudUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @Auther: GIT:liming  -  jybdd
 * @Description - 本类用于: 个人中心
 * @Date: 2019/9/30 17:43
 */
@Controller
@RequestMapping("/test")
@SuppressWarnings("all")
public class UploadController {

    @Autowired
    private IImagesService imagesService;

    @Autowired
    private IMedicalVoService medicalVoService;

    @RequestMapping("/upload")
    @ResponseBody
    public Map upload(MultipartFile file_idcard,HttpSession httpSession,Image image) {
        User user = (User) httpSession.getAttribute("user");
            Map<String,String> map = new HashMap<>();
        try {
            String imgUrl = QiniuCloudUtil.upload(file_idcard.getBytes(),file_idcard.getOriginalFilename());
            map.put("imgUrl", imgUrl);
            //保存图片链接
            image.setLink(imgUrl);
            //保存userid
            image.setUserId(user.getId());
            //保存图片类型 : 1表示身份信息
            image.setType(1);
            imagesService.save(image);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("imgUrl", null);
        }
        return map;

    }

    /**
     * 保存病历
     * @param myfile
     * @param httpSession
     * @param image
     * @return
     */
    @RequestMapping("/uploadmed")
    @ResponseBody
    public Map uploadmed(MultipartFile myfile,HttpSession httpSession,Image image,HttpServletRequest req) {
        User user = (User) httpSession.getAttribute("user");
        Map<String,String> map = new HashMap<>();

        try {
            String imgUrl = QiniuCloudUtil.upload(myfile.getBytes(),myfile.getOriginalFilename());
            map.put("imgUrl", imgUrl);
//            保存病例图片
            image.setLink(imgUrl);
            //保存当前时间
            image.setInowtime(DateUtil.getCurrentTime());
            //保存类型
            image.setType(2);
            //用户id
            image.setUserId(user.getId());
            //保存标题
            String iname = req.getParameter("iname");
            image.setIname(iname);
            medicalVoService.saveMed(image);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("imgUrl", null);
        }
        return map;

    }
}
